package com.yeshen.pmtea.utils;

import com.yeshen.pmtea.beans.News;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;



/**
 * Created by yuyanghui on 16/9/9.
 */
public class ParseJsonToList {

    /**
     * 解析json数据成list(News对象)
     * @param json
     * @return
     */
    public static List<News> parseJsonToNewsList(String json){
        ArrayList<News> dataList = new ArrayList<News>();
        //解析json数据
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            News news;
            for(int i = 0; i < jsonArray.length(); i++){
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                news = new News();
                news.setId(jsonObject1.getString("id"));
                news.setTitle(jsonObject1.getString("title"));
                news.setSource(jsonObject1.getString("source"));
                news.setDescription(jsonObject1.getString("description"));
                news.setWap_thumb(jsonObject1.getString("wap_thumb"));
                news.setCreate_time(jsonObject1.getString("create_time"));
                news.setNickname(jsonObject1.getString("nickname"));
                dataList.add(news);
            }
            return dataList;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
